<!-- 扫码 -->
<template>
  <view class="wrap-scan">
    <u--form labelPosition="left" :model="model1" ref="form1" labelWidth="80">
      <u-form-item label="入库数量" prop borderBottom required>
        <u--input border="none" placeholder="请输入入库数量" v-model="dto.num" type="digit" @input="handleInput"></u--input>
      </u-form-item>
      <u-form-item label="商品名称" prop borderBottom required>
        <u--input border="none" placeholder="请输入商品名称" disabled v-model="dto.tradeName"></u--input>
      </u-form-item>
      <u-form-item label="单位规格" prop borderBottom required>
        <u--input border="none" placeholder="请输入单位规格" disabled v-model="dto.specs"></u--input>
      </u-form-item>
			<view class="btn-box">
      	<view @click="handleCancel" class="btn cancle">取消</view>
      	<view @click="handleSubmit" class="btn sure">保存</view>
    	</view>
    </u--form>

    <u-toast ref="uToast"></u-toast>

  </view>
</template>

<script>
export default {
	name: 'scan',
  components: {
		
	},
	props: {
		guid: {
			type: String,
			default: '',
		}
	},
  data() {
    return {
      dto: {
        batchNoIn: "",
        bookInRootGuid: "",
        bookOutGuid: "",
        bookStatus: 0,
        certificateCode: "",
        certificateGuid: "",
        certificateType: "",
        checkoutImg: "",
        foodTypeId: 0,
        foodTypeName: "",
        guid: "",
        num: "",
        orgGuid: "",
        produceAddress: "",
        produceDate: "",
        produceFactory: "",
        purchaseDate: "",
        quality: 0,
        qualitySpecs: "天",
        specs: "",
        surplusNum: 0,
        tradeName: ""
      }
    };
  },
  onLoad() {
    uni.hideHomeButton();
	},
	mounted() {
		this.getStandingBookOutByGuid();
	},
  methods: {
		getStandingBookOutByGuid() {
      this.$api.chainStandingBookOut.getStandingBookOutByGuid(this.guid).then(({data}) => {
				this.dto = { ...data };
        console.log('保质期');
        this.dto.qualitySpecs = "天"
        this.dto.bookOutGuid = data.guid; 
        console.log(this.dto.quality);
      })
    },
		handleSubmit() {
			console.log(this);
			if (this.dto.num == "" || Number(this.dto.num) == 0) {
        this.$refs.uToast.show({
          message: "请输入入库数量"
        });
        return;
      } else if (!/^(?!0$|0\.00|0\.0|0\d+$)([1-9]?\d+(\.\d*)|(\\s&&[^\\f\\n\\r\\t\\v])|([1-9]*[1-9][0-9]*)?)$/.test(this.dto.num)) {
        this.$refs.uToast.show({
          message: "入库数量, 只能输入正整数和正小数，且不为0，不为空格"
        });
				return
			}
      uni.showLoading({
        mask: true,
        title: "数据提交中..."
			});
			delete this.dto.id;
			delete this.dto.batchNoOut;
			delete this.dto.bookInGuid;
			delete this.dto.clientOrgGuid;
			delete this.dto.clientOrgName;
			delete this.dto.createdAt;
			delete this.dto.deleted;
			delete this.dto.outDate;
			delete this.dto.outNum;
      delete this.dto.updatedAt;
      this.dto.purchaseDate = this.transformTime(Number(new Date())) + " " + "00:00:00";
      // this.dto.orgGuid = this.$storejs.get('orgInfo').guid;
      this.dto.certificateCode = "";
      this.dto.certificateGuid = "";
      this.dto.certificateType = "";
      this.dto.guid = "";
			this.$api.chainStandingBookIn.add(this.dto).then(res => {
          uni.hideLoading();
          this.$refs.uToast.show({
            message: res.message
          });
          setTimeout(() => {
						this.$emit('cancel-scan', false)
          }, 2000);
        })
		},
		handleCancel() {
      this.$emit('cancel-scan', false)
		},
		handleInput(e) {
      console.log(e);
      const inputRule = /[^\d^\.]+/g;
      this.$nextTick(function() {
        this.dto.num = e.replace(inputRule , '');
      })
    },
    transformTime(date){
      var date = new Date(date);
      var datetime =  date.getFullYear() + "-" +(date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1): date.getMonth() + 1)
			+ '-' + (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
      console.log(datetime);
      return datetime;
    },
	}
};
</script>

<style scoped lang="scss">
.wrap-scan {
  background: #fff;
  padding: 20rpx 30rpx;
}
.btn-box {
  margin-top: 100rpx;
	text-align: center;
	display: flex;
}
.btn {
	float: left;
	margin: 0 10px;
	padding: 6px;
	border-radius: 6px;
}
.cancle {
	flex: 1;
	color: #ddd;
	border: 1px solid #ddd;
}
.sure {
	flex: 2;
	background: #3E82FF;
	border: 1px solid #3E82FF;
	color: #fff;
}
</style>
